// 分类
import { topCategory } from "@/api/constants";
import { findAllCategory } from '@/api/category'
export default {
    namespaced: true,// 开启命名空间
    state() {
        return {
            list: topCategory.map(item => ({ name: item }))
        }
    },
    // 修改数据
    mutations: {
        setList(state, payload) {
            state.list = payload
        },
        show(state,id){
            const categoryList = state.list.find(item=>item.id === id)
            categoryList.open = true;
        },
        hide(state,id){
            const categoryList = state.list.find(item=>item.id === id)
            categoryList.open = false;
        }
    },
    // 获取数据
    actions: {
        async getList({ commit }) {
            const { result } = await findAllCategory();
            // 给一级分类加上一个控制二级分类显示隐藏的数据open
            result.forEach(item => {
                item.open = false
            })
            commit('setList', result);
        }
    }
}